package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.plaf.multi.MultiButtonUI;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-25 09:44
 **/
@RestController
@RequestMapping("ordersetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("importOrderSettings")
    public Result importOrderSettings(MultipartFile excelFile) {

        try {
            POIUtils.checkFile(excelFile);
        } catch (IOException e) {
            return new Result(false , e.getMessage());
        }

        try {
            List<String[]> fileDatas = POIUtils.readExcel(excelFile);
            ArrayList<OrderSetting> orderSettings = new ArrayList<>();
            if (fileDatas != null) {
                for (String[] fileData : fileDatas) {
                    OrderSetting orderSetting = new OrderSetting();
                    orderSetting.setNumber(Integer.parseInt(fileData[1]));
                    orderSetting.setOrderDate(DateUtils.parseString2Date(fileData[0], "yyyy/MM/dd"));
                    orderSettings.add(orderSetting);
                }
            }
            orderSettingService.importOrderSettings(orderSettings);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);

        } catch (Exception e) {
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("queryAllOrderSetting")
    public Result queryAllOrderSetting(@RequestParam("date")String date){
        List<Map<String, String>> orderSettings = orderSettingService.queryAllOrderSetting(date);
        return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, orderSettings);
    }

    @RequestMapping("edit")
    public Result edit(@RequestBody OrderSetting orderSetting){

        try {
            orderSettingService.edit(orderSetting);
            return new Result(true, MessageConstant.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_ORDERSETTING_SUCCESS);

        }


    }

}
